package com.datang.yourtomcat02;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-09-16 18:06
 * @notify 包装客户端请求
 * @version 1.0
 */
public class Request implements ServletRequest {
    private InputStream input;
    private String uri;


    public Request(InputStream input) {
        this.input = input;
    }

    /*获取请求资源路径*/
    public String getUri() {
        return uri;
    }

    /*
     * @author 顶风少年
     * @date 2019/9/16 19:04
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @result void
     * @notify 将客户端的请求,读取到字符串
     * version 1.0
     */
    public void parse() {

        StringBuffer request = new StringBuffer(2048);
        int i;
        byte[] buffer = new byte[2048];
        try {
            //将字节流读取到字节数组
            i = input.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
            i = -1;
        }
        //字节数组存放到字符串
        for (int j = 0; j < i; j++) {
            request.append((char) buffer[j]);
        }
        //解析出来请求资源
        uri = parseUri(request.toString());
    }

    /*
     * @author 顶风少年
     * @date 2019/9/16 19:02
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param requestString
     * @result java.lang.String
     * @notify 解析 请求资源路径
     * version 1.0
     */
    private String parseUri(String requestString) {
        int index1, index2;
        //查询第一个空格
        index1 = requestString.indexOf(" ");
        if (index1 != -1) {
            //查询第二个空格
            index2 = requestString.indexOf(" ", index1 + 1);
            if (index2 > index1) {
                //返回请求资源路径
                return requestString.substring(index1 + 1, index2);
            }
        }
        return null;
    }

    public Object getAttribute(String s) {
        return null;
    }

    public Enumeration getAttributeNames() {
        return null;
    }

    public String getCharacterEncoding() {
        return null;
    }

    public void setCharacterEncoding(String s) throws UnsupportedEncodingException {

    }

    public int getContentLength() {
        return 0;
    }

    public String getContentType() {
        return null;
    }

    public ServletInputStream getInputStream() throws IOException {
        return null;
    }

    public String getParameter(String s) {
        return null;
    }

    public Enumeration getParameterNames() {
        return null;
    }

    public String[] getParameterValues(String s) {
        return new String[0];
    }

    public Map getParameterMap() {
        return null;
    }

    public String getProtocol() {
        return null;
    }

    public String getScheme() {
        return null;
    }

    public String getServerName() {
        return null;
    }

    public int getServerPort() {
        return 0;
    }

    public BufferedReader getReader() throws IOException {
        return null;
    }

    public String getRemoteAddr() {
        return null;
    }

    public String getRemoteHost() {
        return null;
    }

    public void setAttribute(String s, Object o) {

    }

    public void removeAttribute(String s) {

    }

    public Locale getLocale() {
        return null;
    }

    public Enumeration getLocales() {
        return null;
    }

    public boolean isSecure() {
        return false;
    }

    public RequestDispatcher getRequestDispatcher(String s) {
        return null;
    }

    public String getRealPath(String s) {
        return null;
    }
}
